<template>
	<view>
		<view class="block__title">被访信息</view>

		<view class="cu-form-group">
			<view class="title">被访人员</view>
			<input v-model="proprietor" style="text-align:right" placeholder="请输入被访人员姓名"></input>
		</view>

		<view class="block__title">访客信息</view>
		<view class="cu-form-group">
			<view class="title">访客姓名</view>
			<input v-model="visitorName" placeholder="请输入访客姓名" class="text-right"></input>
		</view>

		<view class="cu-form-group">
			<view class="title">访客联系方式</view>
			<input v-model="phone" placeholder="请输入访客联系方式" class="text-right"></input>
		</view>

		<view class="cu-form-group">
			<view class="title">随行人数</view>
			<input v-model="entourageNumber" placeholder="请输入随行人数" class="text-right"></input>
		</view>
		
		<view class="block__title">访客事由</view>
		<view class="cu-form-group">
			<textarea v-model="visitorReason" placeholder="请输入"></textarea>
		</view>


		<view class="button_up_blank"></view>
		<view class="noti">
			<view class="text-red text-sm margin-left-sm">*预计来访时和预计离开时间间隔不能超过24小时</view>
			<view class="text-red text-sm margin-left-sm">*预约车辆自开始时间起计算，免费停放{{freeInfo.freeTime}}分钟 </view>
			<view class="text-red text-sm margin-left-sm" v-show="freeInfo.freeTimes > 0">
				*预约车辆每天限制{{freeInfo.freeTimes}}次登记，超过次数系统不会审核</view>
		</view>

		<view class="flex flex-direction">
			<button class="bg-green margin-tb-sm lg confirm_btn" @click="submitVisit()">提交</button>
		</view>
	</view>
</template>

<script>
	// pages/visit/addVisit.js
	import context from '../../lib/java110/Java110Context.js';
	const constant = context.constant;
	const factory = context.factory;
	// import {listOwnerVisit, saveAddVisit}  from '../../api/visit/visit.js'

	// import CarNumber from '../../components/codecook-carnumber/components/codecook-carnumber/codecook-carnumber.vue'
	import {
		formatTimeNow
	} from '../../lib/java110/utils/DateUtil.js'
	import {
		checkPhoneNumber,
		checkStrLength
	} from '../../lib/java110/utils/StringUtil.js'
	import * as TanslateImage from '../../lib/java110/utils/translate-image.js';

	export default {
		data() {
			return {
				visitorReason:"",
				proprietor: '',
				visitorName: '',
				phone: '',
				visitGender: 0,
				visitGenderIndex: 0,
				carNum: '',
				entourageNumber: '',
				visitTime: '',
				departureTime: '',
				visitCase: '',
				reasonType: 0,
				reasonTypeIndex: 0,
				roomCloums: [],
				roomIdArr: [],
				ownerId: '',
				userId: '',
				communityId: '',
				roomId: '',
				roomName: '',
				imgList: [],
				photos: [],
				freeInfo: {}
			};
		},

		components: {
			// CarNumber
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			let that = this;
			context.onLoad(options);
			this.visitTime = formatTimeNow();
			// 房屋信息
		},

		methods: {
			_queryFreeInfo: function() {
				let _that = this;
				let _objData = {
					page: 1,
					row: 1,
					communityId: _that.communityId,
					addVisitType: 'initAddVisitParameter',
				}
				listOwnerVisit(_objData).then(function(info) {
					_that.freeInfo = info;
				})
			},
			// 选择性别
			visitGenderChange: function(e) {
				this.visitGenderIndex = e.target.value
				this.visitGender = this.visitGenderScopes[this.visitGenderIndex].value;
			},
			reasonTypeChange: function(e) {
				this.reasonTypeIndex = e.target.value
				this.reasonType = this.reasonTypeScopes[this.reasonTypeIndex].value;
			},
			roomChange: function(e) {
				this.roomId = this.roomIdArr[e.detail.value];
				this.roomName = this.roomCloums[e.detail.value];
			},
			// 上传图片
			deleteImage: function(e) {
				this.imgList.splice(e, 1);
				this.photos.splice(e, 1);
			},
			ChooseImage: function(e) {
				let that = this;
				wx.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						that.imgList.push(res.tempFilePaths[0]);
						var tempFilePaths = res.tempFilePaths[0]

						//#ifdef H5
						// TanslateImage.translate(tempFilePaths, (url) => {
						// 	that.photos.push(url);
						// })
						//#endif

						//#ifdef MP-WEIXIN
						factory.base64.urlTobase64(tempFilePaths).then(function(_res) {
							that.photos.push(_res);
						});
						//#endif
					}
				});
			},
			// 提交
			submitVisit: function() {
				
				
				
				// let obj = {

				// 	endTime: '',
				// 	entourageNumber: this.entourageNumber,
				// 	houseId: 0,
				// 	id: 1,
				// 	phone: this.phone,
				// 	plateNumber: this.plateNumber,
				// 	plateNumber: "陕A1254",
				// 	proprietorId: 1,
				// 	startTime: '',
				// 	visitorName: _that.visitorName,
				// 	visitorReason: "1001"

				// };
				// // // if (this.photos.length > 0) {
				// // // 	obj.photo = this.photos[0];
				// // // }

				// let msg = "";
				// if (obj.proprietor == "") {
				// 	msg = "请填写您的姓名";
				// } else if (obj.visitorName == "") {
				// 	msg = "请填写访客姓名";
				// } else if (checkStrLength(obj.visitorName) > 10) {
				// 	msg = "访客姓名过长";
				// } else if (obj.phone == "") {
				// 	msg = "请填写手机号";
				// 	// } else if (!checkPhoneNumber(obj.phoneNumber)) {
				// 	// 	msg = "手机号有误";
				// } else if (obj.entourageNumber === "") {
				// 	msg = "请填写随行人数";
				// } else if (!/^\d+$/.test(obj.entourageNumber) && obj.entourageNumber != 0) {
				// 	msg = "随行人数有误";
				// } else {
				// 	console.log("新增访客")
				// }

				// if (msg != "") {
				// 	wx.showToast({
				// 		title: msg,
				// 		icon: 'none',
				// 		duration: 2000
				// 	});
				// 	return;
				// } else {
				let _that = this;
				let userIdData = uni.getStorageSync("userInfo");
				_that.userId = userIdData.id
				let proprietorIdData = uni.getStorageSync("commonUser");
				_that.proprietorId = proprietorIdData.id
				
				let communityIdNum = uni.getStorageSync("_selectCommunity");
				_that.communityId = communityIdNum.id
				
				uni.request({
					url: constant.url.saveAddVisit,
					header: context.getHeaders(),
					method: "POST",
					data: {
						endTime: '',
						entourageNumber: _that.entourageNumber,
						houseId: "",
						id: '',
						communityId:_that.communityId,
						proprietorId:_that.proprietorId,
						phone: _that.phone,
						plateNumber: _that.plateNumber,
						plateNumber: "",
						userId: _that.userId,
						startTime: '',
						visitorName: _that.visitorName,
						visitorReason: _that.visitorReason,
					},
					success: (res) => {
						uni.navigateBack({
							url:"./visitList"
						})
						// this.repairTypes = res.data.data
						// if (res.statusCode == 200) {
							// let visits = res.data.data;
							// _that.visits = visits;
							// if (_that.visits.length < 1) {
							// 	_that.noData = true;
							// }
							// console.log("_that.visits", _that.visits)
						// }
					},
					fail: function(e) {
						wx.showToast({
							title: "服务器异常了",
							icon: 'none',
							duration: 2000
						});
					}
				});
				// }
			}
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {}
		// }
	};
</script>
<style>
	@import "./addVisit.css";
</style>
